package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsGateway;
import com.caiyi.financial.nirvana.sms.enums.*;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsGatewayMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryGatewayBean;
import com.caiyi.financial.nirvana.sms.vo.SmsGatewayVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsGatewayService extends BaseService<SmsGateway> {

    @Autowired
    private SmsGatewayMapper smsGatewayMapper;

    public int addGateway(SmsGateway smsGateway) {
        int insertCount = this.insertByEntitySelective(smsGateway);
        SmsGateway gateway = this.selectByPrimaryKey(smsGateway);
        //redis中添加网关
        String key = String.format(JedisKey.SMS_GATEWAY, smsGateway.getId());
        JedisService.set(key, JSON.toJSONString(smsGateway));
        Map map = new HashMap();
        map.put("method", SubscribeEnum.ADDGATEWAY.method);
        map.put("gatewayId", gateway.getId());
        //发布一条添加通知
        JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        return insertCount;
    }

    public int deleteGateway(Integer id) {
        SmsGateway smsGateway = new SmsGateway();
        smsGateway.setId(id);
        int deleteCount = this.deleteByPrimaryKey(smsGateway);
        //删除redis中gateway
        String key = String.format(JedisKey.SMS_GATEWAY, id);
        JedisService.del(key);
        //发布一条删除通知
        Map map = new HashMap();
        map.put("method", SubscribeEnum.DELETEGATEWAY.method);
        map.put("gatewayId", id);
        //发布一条添加通知
        JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        logger.info("deleteByGateway id {}", id);
        return deleteCount;
    }

    public int updateGateway(SmsGateway smsGateway) {
        int updateCount = this.updateByEntitySelective(smsGateway);
        smsGateway = selectByPrimaryKey(smsGateway);
        int status = smsGateway.getStatus();
        //gateway状态为启用时更新redis
        if (status == SmsGatewayStatusEnum.ENABLE.index) {
            //redis中更新网关
            String key = String.format(JedisKey.SMS_GATEWAY, smsGateway.getId());
            JedisService.set(key, JSON.toJSONString(smsGateway));
            //发布一条更新通知
            Map map = new HashMap();
            map.put("method", SubscribeEnum.UPDATEGATEWAY.method);
            map.put("gatewayId", smsGateway.getId());
            //发布一条添加通知
            JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        }
        return updateCount;
    }

    public PageBean<SmsGatewayVo> getGatewayByParams(QueryGatewayBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsGatewayVo> list = smsGatewayMapper.selectGatewayListByParams(bean);
        Page<SmsGatewayVo> page = (Page<SmsGatewayVo>) list;
        List<SmsGatewayVo> listSmsGatewayVo = new ArrayList<>();
        for (SmsGatewayVo smsGatewayVo : list) {
            String typeName = null;
            String protocolName = null;
            String statusName = null;
            Integer type = smsGatewayVo.getType();
            Integer protocol = smsGatewayVo.getProtocol();
            Integer status = smsGatewayVo.getStatus();
            for (SmsGatewayTypeEnum e : SmsGatewayTypeEnum.values()) {
                if (type.equals(e.index)) {
                    typeName = e.desc;
                }
            }
            for (SmsGatewayProtocolEnum e : SmsGatewayProtocolEnum.values()) {
                if (protocol.equals(e.index)) {
                    protocolName = e.desc;
                }
            }
            for (SmsGatewayStatusEnum e : SmsGatewayStatusEnum.values()) {
                if (status.equals(e.index)) {
                    statusName = e.desc;
                }
            }
            String balance = "0";
            try {
                balance = JedisService.hget(JedisKey.SMS_GATEWAY_BALANCE, smsGatewayVo.getId() + "");
            } catch (Exception e) {

            }
            smsGatewayVo.setBalance(balance);
            smsGatewayVo.setTypeName(typeName);
            smsGatewayVo.setProtocolName(protocolName);
            smsGatewayVo.setStatusName(statusName);
            listSmsGatewayVo.add(smsGatewayVo);

        }
        PageBean<SmsGatewayVo> pageBean = new PageBean<>(listSmsGatewayVo);
        pageBean.setPages(page.getPages());
        pageBean.setTotal(page.getTotal());
        return pageBean;
    }

    public List<SmsGateway> queryByOperator(Integer operator) {
        List<SmsGateway> list = smsGatewayMapper.queryByOperator(operator);
        return list;
    }


    public int updateStatus(Integer id) {
        SmsGateway gateway = new SmsGateway();
        gateway.setId(id);
        int updateCount = smsGatewayMapper.changeStatus(id);
        //redis中更新网关状态
        gateway = this.selectByPrimaryKey(gateway);
        Integer status = gateway.getStatus();
        String key = String.format(JedisKey.SMS_GATEWAY, gateway.getId());
        Map map = new HashMap();
        map.put("gatewayId", id);
        if (status == SmsGatewayStatusEnum.ENABLE.index) {
            //如果状态是启用则在redis中添加gateway
            JedisService.set(key, JSON.toJSONString(gateway));
            //发布一条启用通知
            map.put("method", SubscribeEnum.ENABLEGATEWAY.method);
            JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        } else {
            //如果状态是禁用则在redis中删除gateway
            JedisService.del(key);
            //发布一条禁用通知
            map.put("method", SubscribeEnum.DISABLEGATEWAY.method);
            JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        }
        logger.info(" gateway changeStatus id {}", updateCount);
        return updateCount;
    }


    public int updateStatus(Integer id, Integer status) {
        if (status != SmsGatewayStatusEnum.ENABLE.index && status != SmsGatewayStatusEnum.DISABLE.index) {
            YyAssert.isFalse(true, ReturnCodeEnum.PARAMS_ERROR.code, "参数错误");
        }
        SmsGateway gateway = new SmsGateway();
        gateway.setId(id);
        int updateCount = smsGatewayMapper.updateStatus(id, status);
        Map map = new HashMap();
        map.put("gatewayId", id);
        //redis中更新网关状态
        gateway = this.selectByPrimaryKey(gateway);
        String key = String.format(JedisKey.SMS_GATEWAY, gateway.getId());
        if (status == SmsGatewayStatusEnum.ENABLE.index) {
            //如果状态是启用则在redis中添加gateway
            JedisService.set(key, JSON.toJSONString(gateway));
            //发布一条启用通知
            map.put("method", SubscribeEnum.ENABLEGATEWAY.method);
            JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        }
        if (status == SmsGatewayStatusEnum.DISABLE.index) {
            //如果状态是禁用则在redis中删除gateway
            JedisService.del(key);
            //发布一条禁用通知
            map.put("method", SubscribeEnum.DISABLEGATEWAY.method);
            JedisService.publish(JedisKey.SMS_SUBSCRIBE_GATEWAY_ACTION, JSON.toJSONString(map));
        }
        logger.info(" gateway updateStatus id {}", updateCount);
        return updateCount;
    }

    public List<Map> getGatewayId() {
        List<Map> list = smsGatewayMapper.getGatewayId();
        return list;
    }

}
